import Hero from "../../core_wq/game/player/Hero"
import GameCameraNum from "./GameCameraNum";
import LayerManager from "../manager/LayerManager";
import SceneManager from "../manager/SceneManager";
import { GameStateType } from "./GameStateType";
import { ui } from "../../ui/layaMaxUI";
import GameStage from "./GameStage";
import GameEvent from "./GameEvent";
import GameOverView from "../../module/gameOver/GameOverView";
import LoseView from "../../module/gameOver/LoseView";
import PlayerScript from "../component/PlayerScript";
import Monster from "./player/Monster";
import MonsterData from "./data/MonsterData";
import HeroVO from "../db/vo/HeroVO";
import GlobalData from "../db/GlobalData";
import SoundMgr from "../sound/SoundMgr";
import SoundType from "../sound/SoundType";
import SoundVO from "../db/vo/SoundVO";
import GameCheat from "./GameCheat";
import GameConfig from "../../GameConfig";
import AppConfig from "../config/AppConfig";
import GameConfigVO from "../db/vo/GameConfigVO";



export default class Game {

    static hero: Hero = new Hero();

    static monster: Monster = new Monster();

    static state: GameStateType = GameStateType.IDLE;

    static layer3d: Laya.Sprite3D = new Laya.Sprite3D();

    static scene3d: Laya.Scene3D;

    static cameraNum: GameCameraNum;

    static camera: Laya.Camera;

    static layerManager: LayerManager;

    static sceneManager: SceneManager;

    static map: Laya.Sprite;

    static flyLayer: Laya.Sprite = new Laya.Sprite();

    static footLayer: Laya.Sprite = new Laya.Sprite();

    static hpLayer: Laya.Sprite = new Laya.Sprite();

    static monsterResClones: Laya.Sprite3D[] = [];

    static isPress: boolean = false;

    static gameStage: GameStage = new GameStage();

    static projectViewMat: Laya.Matrix4x4 = new Laya.Matrix4x4();

    static isWin: boolean;

    static heroVO: HeroVO;

    static gameCheat: GameCheat = new GameCheat();

    static gameConfigVO: GameConfigVO;


    static init() {
        // console.log("init..!!")
        Game.sceneManager = new SceneManager();
        Game.layerManager = new LayerManager();
        Laya.stage.addChild(Game.layerManager);

        Laya.stage.on(GameEvent.GAME_OVER, Game, Game.gameOver);
        Laya.stage.on(GameEvent.GO_HOME, Game, Game.goHome);
        Laya.stage.on(GameEvent.GAME_AGAIN, Game, Game.again);
        Laya.stage.on(GameEvent.START_GAME, Game, Game.startGame);

        Game.gameConfigVO = GlobalData.getData(GlobalData.GameConfigVO, 1);

        Game.gameStage.init();
        Game.preLoad();

        if (AppConfig.isDebug) {
            Game.gameCheat.cheat();
        }
    }



    static preLoad() {
        for (var index = 0; index < 15; index++) {
            let imgFly: Laya.Image = Laya.Pool.getItemByClass("p_Image_fly", Laya.Image);
            if (imgFly == null) {
                imgFly = new Laya.Image();
            }
            Laya.Pool.recover("p_Image_fly", imgFly);
        }
    }

    static goHome() {

        if (Game.state !== GameStateType.BATTLING) {
            Game.sceneManager.main.mainUI.loadRes();
            Game.reset();
            Game.sceneManager.main.mainUI.show();
        }
    }

    static again() {
        Game.state = GameStateType.IDLE;
        Game.monster.reset();
        Game.clearRes();
        Game.gameStage.reset();
        Game.sceneManager.main.mainUI.refreshBg();
        Game.sceneManager.main.mainUI.bgSprite.alpha = 1;
        Game.hero.init();
        Game.startGame();
        Game.sceneManager.main.gameStageHpView.reset();
    }

    static reset(): void {
        Game.state = GameStateType.IDLE;
        Game.heroVO = GlobalData.getData(GlobalData.HeroVO, Game.hero.playerData.mapLevel);
        Game.monster.reset();
        Game.clearRes();
        Game.gameStage.reset();
        Game.sceneManager.main.mainUI.refreshBg();
        Game.hero.init();
        Game.hero.initBullet();
        Game.sceneManager.main.mainUI.loadRes();
        Game.sceneManager.main.gameStageHpView.reset();

    }

    static clearRes() {
        Game.footLayer.removeChildren();
        Game.layer3d.removeChildren();
        Game.hpLayer.removeChildren();
        Game.flyLayer.removeChildren();
        Game.monsterResClones.map((monsterRes: Laya.Sprite3D) => {
            monsterRes.destroy();
        });
        Game.hero.bullet.clearPool();
    }

    static startGame() {
        Game.hero.bullet.cacheBullet();
        Game.hero.resetFireTime();
        Game.state = GameStateType.BATTLING;
        Game.gameStage.start();
    }

    static gameOver(data) {
        // console.log("gameover,", data)

        if (Game.state === GameStateType.GAME_OVER) {
            return;
        }
        Game.state = GameStateType.GAME_OVER;
        Game.isWin = data.isWin;
        if (Game.isWin) {
            Game.layerManager.panelLayer.addChild(new GameOverView());
        }
        else {
            Game.layerManager.panelLayer.addChild(new LoseView());
        }
    }

    // static onKillMonster(monsterData:MonsterData){
    //     console.log("event.onKillMonster",monsterData);
    //     Laya.Tween.to(Game.sceneManager.main.,{width:0},300,null,new Laya.Handler(this,this.onCom));

    // }







}